import Vue from 'vue'
import Element from 'element-ui'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Camera from '../components/Camera.vue'
import Stage from '../components/Stage.vue'
import System from '../components/System.vue'
import Manage from '../components/Manage.vue'
import AnotherFile from '../components/anotherFile.vue'

Vue.use(VueRouter)
Vue.use(Element)

const router = new VueRouter({
  //mode: 'history',
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/secret', component: AnotherFile },
    {
      path: '/home',
      component: Home,
      redirect: '/camera',
      children: [
        { path: '/camera', component: Camera },
        { path: '/stage', component: Stage },
        { path: '/system', component: System },
        { path: '/manage', component: Manage },
      ]
    }
  ]
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
